import mysql, { Pool } from 'mysql2/promise';
import ENV from '@src/common/constants/ENV';

// 创建数据库连接池
const pool : Pool = mysql.createPool({
  host: ENV.DB_HOST,
  port: parseInt(ENV.DB_PORT || '3306'),
  user: ENV.DB_USER,
  password: ENV.DB_PASSWORD,
  database: ENV.DB_NAME,
  waitForConnections: true,
  connectionLimit: 10,
  queueLimit: 0,
});

// 测试数据库连接
async function testConnection() {
  try {
    const connection = await pool.getConnection();
    console.log('数据库连接成功');
    connection.release();
  } catch (error) {
    console.error('数据库连接失败', error);
    process.exit(1);
  }
}

export default {
  pool,
  testConnection,
} as const;